package com.huanglj.himalaya.presenters;

import androidx.annotation.NonNull;

import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.data.dao.ISubDaoCallback;
import com.huanglj.himalaya.data.dao.SubscriptionDao;
import com.huanglj.himalaya.interfaces.ISubscriptionCallback;
import com.huanglj.himalaya.interfaces.ISubscriptionPresenter;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;


public class SubscriptionPresenter implements ISubscriptionPresenter, ISubDaoCallback {

    private final SubscriptionDao mSubscriptionDao;
    private Map<Long,Album> mData = new HashMap<>();
    private List<ISubscriptionCallback> mCallBack = new ArrayList<>();
    private SubscriptionPresenter(){
        mSubscriptionDao = SubscriptionDao.getInstance();
        mSubscriptionDao.registerViewCallBack(this);
        mSubscriptionDao.getAlbumList();
    }
    public static SubscriptionPresenter sInstance = null;

    public static SubscriptionPresenter getInstance(){
        if (sInstance == null) {
            synchronized (SubscriptionPresenter.class){
                if (sInstance == null) {
                    sInstance = new SubscriptionPresenter();
                }
            }
        }
        return sInstance;
    }
    @Override
    public void addSubscription(final Album album) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.addAlbum(album);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void deleteSubscription(final Album album) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.deleteAlbum(album);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void getSubscriptionList() {
        listSubscriptions();
    }

    private void listSubscriptions() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) {
                //只调用，不处理结果
                if (mSubscriptionDao != null) {
                    mSubscriptionDao.getAlbumList();
                }
                
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public boolean isSub(Album album) {
        Album result = mData.get(album.getId());
        return result != null;
    }

    @Override
    public void unRegisterViewCallBack(ISubscriptionCallback iSubscriptionCallback) {
        mCallBack.remove(iSubscriptionCallback);
    }

    @Override
    public void registerViewCallBack(ISubscriptionCallback iSubscriptionCallback) {
        mCallBack.add(iSubscriptionCallback);
    }

    @Override
    public void onAddResult(final boolean isSuccess) {
        listSubscriptions();
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionCallback callback : mCallBack) {
                    callback.onAddResult(isSuccess);
                }
            }
        });
    }

    @Override
    public void onDeleteResult(final boolean isSuccess) {
        listSubscriptions();
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionCallback callback : mCallBack) {
                    callback.onDeleteResult(isSuccess);
                }
            }
        });
    }

    @Override
    public void onGetListResult(final List<Album> albums) {
        mData.clear();
        for (Album album : albums) {
            mData.put(album.getId(),album);
        }
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionCallback callback : mCallBack) {
                    callback.onGetListLoad(albums);
                }
            }
        });
    }
}
